//
//  LJFHomeController.m
//  SevenZhou
//
//  Created by qianfeng on 15/9/14.
//  Copyright (c) 2015年 ljf. All rights reserved.
//

#import "LJFHomeController.h"
#import "LJFCustom1Cell.h"
#import "LJFCustom2Cell.h"
#import "LJFHttpManager.h"
#import "LJFSubjects.h"
#import "UIImageView+AFNetworking.h"
#import "LJFHeadView.h"
#import "LJFCustomCell.h"
#import "LJFDetailController.h"
#import "LJFCustomController.h"
#import "LJFSearchResultController.h"
#import "LJFCategoryModel.h"
#import "LJFContinentModel.h"
#import "LJFSearchController.h"
#import "LJFWedController.h"
#import "MBProgressHUD.h"
#import "MJRefresh.h"

@interface LJFHomeController ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate,LJFCustomCellDelegate,LJFCustomCellMyDelegate,headViewImageClickDelegate,MJRefreshBaseViewDelegate>
{
    NSMutableArray *_dataArr;
    LJFHeadView *_headView;
    
    BOOL _destIsOver;
    BOOL _isRefresh;
    
    MBProgressHUD *_HUD;
    
    MJRefreshHeaderView *_refresHeadView;
}

@property (weak, nonatomic) IBOutlet UITableView *myTableView;


@end

@implementation LJFHomeController

#pragma mark - 初始化

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self)
    {
        _dataArr =[[NSMutableArray alloc] init];
    }
    return self;
}

#pragma mark - 加载View

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self showProgress];
    
    [self createRefreshView];

    [self loadSevenZhouDataFromNet];
    
    [self loadCategoryDataFromNet];
    
    [self loadDestDataFromNet];
    
    [self createHeadView];
    
    [_myTableView registerNib:[UINib nibWithNibName:@"LJFCustomCell" bundle:nil] forCellReuseIdentifier:@"LJFCustomCell"];
}

- (void)showProgress
{
    _HUD = [[MBProgressHUD alloc] initWithView:self.view];
    _HUD.labelText = @"正在努力加载中";
    
    [self.view addSubview:_HUD];
    
    [_HUD show:YES];
}

- (void)createRefreshView
{
    _refresHeadView = [[MJRefreshHeaderView alloc] init];
    _refresHeadView.scrollView = _myTableView;
    _refresHeadView.delegate = self;
}

- (void)dealloc
{
    [_refresHeadView free];
}

- (IBAction)pushSearch:(id)sender
{
    LJFSearchController *search = [self.storyboard instantiateViewControllerWithIdentifier:@"LJFSearchController"];
    search.isFristPush = YES;
    [self.navigationController pushViewController:search animated:YES];
}


#pragma mark - 点击处理事件

- (IBAction)actionArea:(id)sender
{
    LJFCustomController *tabarControl = (LJFCustomController *)self.tabBarController;
    
    NSArray *arr = tabarControl.tabBarView.subviews;
    
    for (UIButton *button in arr)
    {
        button.selected = NO;
    }
    
    UIButton *button = arr[3];
    button.selected = YES;
    
    tabarControl.selectedIndex = 3;
}

- (IBAction)SearchTypeAction:(UIButton *)sender
{
    NSInteger index = sender.tag - 121;
    
    LJFCategoryModel *model = _CategoryArr[index];
    
    LJFSearchResultController *searchResult = [self.storyboard instantiateViewControllerWithIdentifier:@"LJFSearchResultController"];
    searchResult.category_id = model.category_id;
    searchResult.sort_by = @"top_sale";
    searchResult.cateoryIndex = index+1;
    
    [self.navigationController pushViewController:searchResult animated:YES];
}

#pragma mark -LJFCustomCellMyDelegate

- (void)LJFCustomCell:(LJFCustomCell *)cell SearchTypeAreaWithClickTag:(NSInteger)tag
{
    if (_destIsOver)
    {
        NSString *destid = [_DestArr[tag] Myid];
        NSString *name = [_DestArr[tag] name];
        
        LJFSearchResultController *searchResult = [self.storyboard instantiateViewControllerWithIdentifier:@"LJFSearchResultController"];
        searchResult.dest_id = destid;
        searchResult.sort_by = @"top_sale";
        searchResult.destName = name;
        
        [self.navigationController pushViewController:searchResult animated:YES];

    }
}

#pragma mark -LJFCustomCellDelegate

- (void)LJFCustomCellWith:(LJFCustom2Cell *)customcell andImageTag:(NSUInteger)tag
{
    NSUInteger row = (tag-200)/10;
    NSUInteger position = (tag-200)%10;
    
    LJFScenicModel *model = [_dataArr[row] goods_list][position];
    
    LJFDetailController *detail = [self.storyboard instantiateViewControllerWithIdentifier:@"LJFDetailController"];
    detail.goodsid = model.goods_id;
    
    [self.navigationController pushViewController:detail animated:YES];
    
}

# pragma mark -创建表头

- (void)createHeadView
{
    _headView = [[LJFHeadView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 150)];
    _headView.mydelegate = self;
    
    _myTableView.tableHeaderView = _headView;
}

#pragma mark -headViewImageClickDelegate

- (void)headViewImageClick:(LJFHeadView *)head WithUrl:(NSString *)url
{
    LJFWedController *wed = [self.storyboard instantiateViewControllerWithIdentifier:@"LJFWedController"];
    wed.urlString = url;
    
    [self.navigationController pushViewController:wed animated:YES];
}

#pragma mark - MJRefreshBaseViewDelegate

-(void)refreshViewBeginRefreshing:(MJRefreshBaseView *)refreshView
{
    _isRefresh = YES;
    [self loadSevenZhouDataFromNet];
}

#pragma  mark - 从网络加载数据

- (void)loadSevenZhouDataFromNet
{
    LJFHttpManager *manager = [LJFHttpManager shareInstance];
    
    [manager getDataFromNetWithUrl:URL_HomeSevenZhou AndParameterStr:URL_HomeSevenZhou_parameter success:^(id responseObj)
    {
        if (_isRefresh)
        {
            [_dataArr removeAllObjects];
            _isRefresh = NO;
        }
        
        NSDictionary *dict = responseObj;
        
        for (NSDictionary *dic in dict[@"data"])
        {
            LJFSubjects *model = [[LJFSubjects alloc] initWithNsdictaryion:dic];
            
            [_dataArr addObject:model];
            
            [_HUD hide:YES];
        }
        [self.myTableView reloadData];
        
        [_refresHeadView endRefreshing];
        
    } failure:^(NSError *error)
    {
        NSLog(@"error == %@",error);
    }];
}

- (void)loadCategoryDataFromNet
{
    LJFHttpManager *manager = [LJFHttpManager shareInstance];
    
    [manager getDataFromNetWithUrl:URL_Category AndParameterStr:URL_Category_parameter success:^(id responseObj)
     {
         
         NSDictionary *dict = responseObj;
         
         NSMutableArray *arrM = [[NSMutableArray alloc] init];
         
         for (NSDictionary *dic in dict[@"data"])
         {
             LJFCategoryModel *model = [[LJFCategoryModel alloc] initWithDictaryion:dic];
             
             [arrM addObject:model];
         }
         
         _CategoryArr = [NSArray arrayWithArray:arrM];
         
     } failure:^(NSError *error)
     {
         NSLog(@"error == %@",error);
     }];
}

- (void)loadDestDataFromNet
{
    LJFHttpManager *manager = [LJFHttpManager shareInstance];
    
    [manager getDataFromNetWithUrl:URL_SevenZhouData AndParameterStr:@"" success:^(id responseObj)
     {
         NSDictionary *dict = responseObj;
         
         NSMutableArray *arrM = [[NSMutableArray alloc] init];
         
         for (NSDictionary *dic in dict[@"data"])
         {
             LJFContinentModel *model = [[LJFContinentModel alloc] initWithDictionary:dic];
             
             [arrM addObject:model];
         }
         
         _DestArr = [NSArray arrayWithArray:arrM];
         
         _destIsOver = YES;
         
     } failure:^(NSError *error)
     {
         
         NSLog(@"error == %@",error);
         
     }];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark -UITableViewDataSource,UITableViewDelegate

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0
        )
    {
        return 1;
    }
    else if (section == 1)
    {
        return 1;
    }
    else
    {
        return _dataArr.count;
    }
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (indexPath.section == 1)
    {
        LJFCustom1Cell *cell = [tableView dequeueReusableCellWithIdentifier:@"LJFCustom1Cell"];
        
        return cell;
    }
    else if(indexPath.section == 2)
    {
        LJFSubjects *model = _dataArr[indexPath.row];
        
        LJFCustom2Cell *cell = [tableView dequeueReusableCellWithIdentifier:@"LJFCustom2Cell"];
        
        cell.myDelegate = self;
        
        cell.myTitle.text = [NSString stringWithFormat:@"#%@#",model.class_title];
        cell.myContents.text = model.class_text;
        
        [cell.imgView1 setImageWithURL:[NSURL URLWithString:[model.goods_list[0] goods_img]] placeholderImage:[UIImage imageNamed:@"default_image.png"]];
        cell.imgView1.tag = 200 + 10*indexPath.row + 0;
        
        [cell.imgView2 setImageWithURL:[NSURL URLWithString:[model.goods_list[1] goods_img]] placeholderImage:[UIImage imageNamed:@"default_image.png"]];
        cell.imgView2.tag = 200 + 10*indexPath.row + 1;
        
        [cell.imgview3 setImageWithURL:[NSURL URLWithString:[model.goods_list[2] goods_img]] placeholderImage:[UIImage imageNamed:@"default_image.png"]];
        cell.imgview3.tag = 200 + 10*indexPath.row + 2;
        
        [cell.imgView4 setImageWithURL:[NSURL URLWithString:[model.goods_list[3] goods_img]] placeholderImage:[UIImage imageNamed:@"default_image.png"]];
        cell.imgView4.tag = 200 + 10*indexPath.row + 3;
        
        [cell.imgView5 setImageWithURL:[NSURL URLWithString:[model.goods_list[4] goods_img]] placeholderImage:[UIImage imageNamed:@"default_image.png"]];
        cell.imgView5.tag = 200 + 10*indexPath.row + 4;
        
        [cell.imgView6 setImageWithURL:[NSURL URLWithString:[model.goods_list[5] goods_img]] placeholderImage:[UIImage imageNamed:@"default_image.png"]];
        cell.imgView6.tag = 200 + 10*indexPath.row + 5;
        
        [cell.imgView7 setImageWithURL:[NSURL URLWithString:[model.goods_list[6] goods_img]] placeholderImage:[UIImage imageNamed:@"default_image.png"]];
        cell.imgView7.tag = 200 + 10*indexPath.row + 6;
        
        [cell.imgView8 setImageWithURL:[NSURL URLWithString:[model.goods_list[7] goods_img]] placeholderImage:[UIImage imageNamed:@"default_image.png"]];
        cell.imgView8.tag = 200 + 10*indexPath.row + 7;
        
        cell.label1.text = [model.goods_list[0] name];
        cell.label2.text = [model.goods_list[1] name];
        cell.label3.text = [model.goods_list[2] name];
        cell.label4.text = [model.goods_list[3] name];
        cell.label5.text = [model.goods_list[4] name];
        cell.label6.text = [model.goods_list[5] name];
        cell.label7.text = [model.goods_list[6] name];
        cell.label8.text = [model.goods_list[7] name];
        
        return cell;
    }
    else
    {
        LJFCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LJFCustomCell"];
        cell.mydeledate = self;
        
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 1)
    {
        return 183;
    }
    else if(indexPath.section == 2)
    {
        return 290;
    }
    else
    {
        return 70;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == 0)
    {
        return 4;
    }
    else
    {
        return 1;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 2)
    {
        return 30;
    }
    else
    {
        return 0;
    }
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 2)
    {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30)];
 
        UIImageView *bgimag = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30)];
        bgimag.image = [UIImage imageNamed:@"cell_bg_content.png"];
        [view addSubview:bgimag];
        
        UIImageView *imagView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 0, 100, 30)];
        imagView.image = [UIImage imageNamed:@"index_service_list_top_title"];
        [view addSubview:imagView];
        
        return view;
    }
    else
    {
        return nil;
    }
}






@end
